package com.flashcards.repository;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import com.flashcards.LogWrapper;
import com.flashcards.auth.UserInfo;

public class UserInfoRepository extends LogWrapper {
    private static final String PERSISTENT_KEY = "flashcards.user_info";
    private SharedPreferences preferences;
    private static UserInfoRepository userInfoRepository;

    protected UserInfoRepository(SharedPreferences preferences) {
        this.preferences = preferences;
    }

    public void save(UserInfo userInfo) {
        SharedPreferences.Editor edit = this.preferences.edit();
        edit.putString(PERSISTENT_KEY, userInfo.serializeAsString());
        edit.commit();
    }

    public UserInfo lookup() {
        String user_info = this.preferences.getString(PERSISTENT_KEY, "");
        i("###User info is ", user_info);
        return UserInfo.deserialize(user_info);
    }

    public static UserInfoRepository INSTANCE(Activity activity) {
        if(userInfoRepository == null) {
            SharedPreferences flashcards = activity.getSharedPreferences("flashcards", Context.MODE_PRIVATE);
            userInfoRepository = new UserInfoRepository(flashcards);
        }
        return userInfoRepository;
    }

    public static UserInfoRepository INSTANCE() {
        return userInfoRepository;
    }
}
